home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / faces.el.z / faces.el
Encoding:
Text File  |  1998-10-28  |  47.1 KB  |  1,268 lines

  1. ;;; faces.el --- Lisp interface to the c "face" structure
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  19. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. ;; Boston, MA 02111-1307, USA.
  21.  
  22. ;;; Commentary:
  23.  
  24. ;; Mostly derived from Lucid.
  25.  
  26. ;;; Code:
  27.  
  28. (eval-when-compile
  29.  ;; These used to be defsubsts, now they're subrs.  Avoid losing if we're
  30.  ;; being compiled with an old Emacs that still has defsubrs in it.
  31.  (put 'face-name 'byte-optimizer nil)
  32.  (put 'face-id 'byte-optimizer nil)
  33.  (put 'face-font 'byte-optimizer nil)
  34.  (put 'face-foreground 'byte-optimizer nil)
  35.  (put 'face-background 'byte-optimizer nil)
  36.  (put 'face-stipple 'byte-optimizer nil)
  37.  (put 'face-underline-p 'byte-optimizer nil)
  38.  (put 'set-face-font 'byte-optimizer nil)
  39.  (put 'set-face-foreground 'byte-optimizer nil)
  40.  (put 'set-face-background 'byte-optimizer nil)
  41.  (put 'set-face-stipple 'byte-optimizer nil)
  42.  (put 'set-face-underline-p 'byte-optimizer nil))
  43.  
  44. ;;;; Functions for manipulating face vectors.
  45.  
  46. ;;; A face vector is a vector of the form:
  47. ;;;    [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
  48.  
  49. ;;; Type checkers.
  50. (defsubst internal-facep (x)
  51.   (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
  52.  
  53. (defun facep (x)
  54.   "Return t if X is a face name or an internal face vector."
  55.   (and (or (internal-facep x)
  56.        (and (symbolp x) (assq x global-face-data)))
  57.        t))
  58.       
  59. (defmacro internal-check-face (face)
  60.   (` (or (internal-facep (, face))
  61.      (signal 'wrong-type-argument (list 'internal-facep (, face))))))
  62.  
  63. ;;; Accessors.
  64. (defun face-name (face)
  65.   "Return the name of face FACE."
  66.   (aref (internal-get-face face) 1))
  67.  
  68. (defun face-id (face)
  69.   "Return the internal ID number of face FACE."
  70.   (aref (internal-get-face face) 2))
  71.  
  72. (defun face-font (face &optional frame)
  73.   "Return the font name of face FACE, or nil if it is unspecified.
  74. If the optional argument FRAME is given, report on face FACE in that frame.
  75. If FRAME is t, report on the defaults for face FACE (for new frames).
  76.   The font default for a face is either nil, or a list
  77.   of the form (bold), (italic) or (bold italic).
  78. If FRAME is omitted or nil, use the selected frame."
  79.   (aref (internal-get-face face frame) 3))
  80.  
  81. (defun face-foreground (face &optional frame)
  82.   "Return the foreground color name of face FACE, or nil if unspecified.
  83. If the optional argument FRAME is given, report on face FACE in that frame.
  84. If FRAME is t, report on the defaults for face FACE (for new frames).
  85. If FRAME is omitted or nil, use the selected frame."
  86.   (aref (internal-get-face face frame) 4))
  87.  
  88. (defun face-background (face &optional frame)
  89.   "Return the background color name of face FACE, or nil if unspecified.
  90. If the optional argument FRAME is given, report on face FACE in that frame.
  91. If FRAME is t, report on the defaults for face FACE (for new frames).
  92. If FRAME is omitted or nil, use the selected frame."
  93.   (aref (internal-get-face face frame) 5))
  94.  
  95. (defun face-stipple (face &optional frame)
  96.  "Return the stipple pixmap name of face FACE, or nil if unspecified.
  97. If the optional argument FRAME is given, report on face FACE in that frame.
  98. If FRAME is t, report on the defaults for face FACE (for new frames).
  99. If FRAME is omitted or nil, use the selected frame."
  100.  (aref (internal-get-face face frame) 6))
  101.  
  102. (defalias 'face-background-pixmap 'face-stipple)
  103.  
  104. (defun face-underline-p (face &optional frame)
  105.  "Return t if face FACE is underlined.
  106. If the optional argument FRAME is given, report on face FACE in that frame.
  107. If FRAME is t, report on the defaults for face FACE (for new frames).
  108. If FRAME is omitted or nil, use the selected frame."
  109.  (aref (internal-get-face face frame) 7))
  110.  
  111.  
  112. ;;; Mutators.
  113.  
  114. (defun set-face-font (face font &optional frame)
  115.   "Change the font of face FACE to FONT (a string).
  116. If the optional FRAME argument is provided, change only
  117. in that frame; otherwise change each frame."
  118.   (interactive (internal-face-interactive "font"))
  119.   (if (stringp font) (setq font (x-resolve-font-name font 'default frame)))
  120.   (internal-set-face-1 face 'font font 3 frame))
  121.  
  122. (defun set-face-foreground (face color &optional frame)
  123.   "Change the foreground color of face FACE to COLOR (a string).
  124. If the optional FRAME argument is provided, change only
  125. in that frame; otherwise change each frame."
  126.   (interactive (internal-face-interactive "foreground"))
  127.   (internal-set-face-1 face 'foreground color 4 frame))
  128.  
  129. (defvar face-default-stipple "gray3" 
  130.   "Default stipple pattern used on monochrome displays.
  131. This stipple pattern is used on monochrome displays
  132. instead of shades of gray for a face background color.
  133. See `set-face-stipple' for possible values for this variable.")
  134.  
  135. (defun face-color-gray-p (color &optional frame)
  136.   "Return t if COLOR is a shade of gray (or white or black).
  137. FRAME specifies the frame and thus the display for interpreting COLOR."
  138.   (let* ((values (x-color-values color frame))
  139.      (r (nth 0 values))
  140.      (g (nth 1 values))
  141.      (b (nth 2 values)))
  142.     (and values
  143.      (< (abs (- r g)) (/ (max 1 (abs r) (abs g)) 20))
  144.      (< (abs (- g b)) (/ (max 1 (abs g) (abs b)) 20))
  145.      (< (abs (- b r)) (/ (max 1 (abs b) (abs r)) 20)))))
  146.  
  147. (defun set-face-background (face color &optional frame)
  148.   "Change the background color of face FACE to COLOR (a string).
  149. If the optional FRAME argument is provided, change only
  150. in that frame; otherwise change each frame."
  151.   (interactive (internal-face-interactive "background"))
  152.   ;; For a specific frame, use gray stipple instead of gray color
  153.   ;; if the display does not support a gray color.
  154.   (if (and frame (not (eq frame t)) color
  155.        ;; Check for support for foreground, not for background!
  156.        ;; face-color-supported-p is smart enough to know
  157.        ;; that grays are "supported" as background
  158.        ;; because we are supposed to use stipple for them!
  159.        (not (face-color-supported-p frame color nil)))
  160.       (set-face-stipple face face-default-stipple frame)
  161.     (if (null frame)
  162.     (let ((frames (frame-list)))
  163.       (while frames
  164.         (set-face-background (face-name face) color (car frames))
  165.         (setq frames (cdr frames)))
  166.       (set-face-background face color t)
  167.       color)
  168.       (internal-set-face-1 face 'background color 5 frame))))
  169.  
  170. (defun set-face-stipple (face pixmap &optional frame)
  171.   "Change the stipple pixmap of face FACE to PIXMAP.
  172. PIXMAP should be a string, the name of a file of pixmap data.
  173. The directories listed in the `x-bitmap-file-path' variable are searched.
  174.  
  175. Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
  176. where WIDTH and HEIGHT are the size in pixels,
  177. and DATA is a string, containing the raw bits of the bitmap.  
  178.  
  179. If the optional FRAME argument is provided, change only
  180. in that frame; otherwise change each frame."
  181.   (interactive (internal-face-interactive "stipple"))
  182.   (internal-set-face-1 face 'background-pixmap pixmap 6 frame))
  183.  
  184. (defalias 'set-face-background-pixmap 'set-face-stipple)
  185.  
  186. (defun set-face-underline-p (face underline-p &optional frame)
  187.   "Specify whether face FACE is underlined.  (Yes if UNDERLINE-P is non-nil.)
  188. If the optional FRAME argument is provided, change only
  189. in that frame; otherwise change each frame."
  190.   (interactive (internal-face-interactive "underline-p" "underlined"))
  191.   (internal-set-face-1 face 'underline underline-p 7 frame))
  192.  
  193. (defun modify-face-read-string (face default name alist)
  194.   (let ((value
  195.      (completing-read
  196.       (if default
  197.           (format "Set face %s %s (default %s): "
  198.               face name (downcase default))
  199.         (format "Set face %s %s: " face name))
  200.       alist)))
  201.     (cond ((equal value "none")
  202.        nil)
  203.       ((equal value "")
  204.        default)
  205.       (t value))))
  206.  
  207. (defun modify-face (face foreground background stipple
  208.             bold-p italic-p underline-p &optional frame)
  209.   "Change the display attributes for face FACE.
  210. If the optional FRAME argument is provided, change only
  211. in that frame; otherwise change each frame.
  212.  
  213. FOREGROUND and BACKGROUND should be a colour name string (or list of strings to
  214. try) or nil.  STIPPLE should be a stipple pattern name string or nil.
  215. If nil, means do not change the display attribute corresponding to that arg.
  216.  
  217. BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
  218. in italic, and underlined, respectively.  If neither nil or t, means do not
  219. change the display attribute corresponding to that arg.
  220.  
  221. If called interactively, prompts for a face name and face attributes."
  222.   (interactive
  223.    (let* ((completion-ignore-case t)
  224.       (face        (symbol-name (read-face-name "Modify face: ")))
  225.       (colors    (mapcar 'list x-colors))
  226.       (stipples    (mapcar 'list (apply 'nconc
  227.                         (mapcar 'directory-files
  228.                             x-bitmap-file-path))))
  229.       (foreground    (modify-face-read-string
  230.              face (face-foreground (intern face))
  231.              "foreground" colors))
  232.       (background    (modify-face-read-string
  233.              face (face-background (intern face))
  234.              "background" colors))
  235.       (stipple    (modify-face-read-string
  236.              face (face-stipple (intern face))
  237.              "stipple" stipples))
  238.       (bold-p    (y-or-n-p (concat "Set face " face " bold ")))
  239.       (italic-p    (y-or-n-p (concat "Set face " face " italic ")))
  240.       (underline-p    (y-or-n-p (concat "Set face " face " underline ")))
  241.       (all-frames-p    (y-or-n-p (concat "Modify face " face " in all frames "))))
  242.      (message "Face %s: %s" face
  243.       (mapconcat 'identity
  244.        (delq nil
  245.     (list (and foreground (concat (downcase foreground) " foreground"))
  246.           (and background (concat (downcase background) " background"))
  247.           (and stipple (concat (downcase stipple) " stipple"))
  248.           (and bold-p "bold") (and italic-p "italic")
  249.           (and underline-p "underline"))) ", "))
  250.      (list (intern face) foreground background stipple
  251.        bold-p italic-p underline-p
  252.        (if all-frames-p nil (selected-frame)))))
  253.   (condition-case nil
  254.       (face-try-color-list 'set-face-foreground face foreground frame)
  255.     (error nil))
  256.   (condition-case nil
  257.       (face-try-color-list 'set-face-background face background frame)
  258.     (error nil))
  259.   (condition-case nil
  260.       (set-face-stipple face stipple frame)
  261.     (error nil))
  262.   (cond ((eq bold-p nil) (make-face-unbold face frame t))
  263.     ((eq bold-p t) (make-face-bold face frame t)))
  264.   (cond ((eq italic-p nil) (make-face-unitalic face frame t))
  265.     ((eq italic-p t) (make-face-italic face frame t)))
  266.   (if (memq underline-p '(nil t))
  267.       (set-face-underline-p face underline-p frame))
  268.   (and (interactive-p) (redraw-display)))
  269.  
  270. ;;;; Associating face names (symbols) with their face vectors.
  271.  
  272. (defvar global-face-data nil
  273.   "Internal data for face support functions.  Not for external use.
  274. This is an alist associating face names with the default values for
  275. their parameters.  Newly created frames get their data from here.")
  276.  
  277. (defun face-list ()
  278.   "Returns a list of all defined face names."
  279.   (mapcar 'car global-face-data))
  280.  
  281. (defun internal-find-face (name &optional frame)
  282.   "Retrieve the face named NAME.  Return nil if there is no such face.
  283. If the optional argument FRAME is given, this gets the face NAME for
  284. that frame; otherwise, it uses the selected frame.
  285. If FRAME is the symbol t, then the global, non-frame face is returned.
  286. If NAME is already a face, it is simply returned."
  287.   (if (and (eq frame t) (not (symbolp name)))
  288.       (setq name (face-name name)))
  289.   (if (symbolp name)
  290.       (cdr (assq name
  291.          (if (eq frame t)
  292.              global-face-data
  293.            (frame-face-alist (or frame (selected-frame))))))
  294.     (internal-check-face name)
  295.     name))
  296.  
  297. (defun internal-get-face (name &optional frame)
  298.   "Retrieve the face named NAME; error if there is none.
  299. If the optional argument FRAME is given, this gets the face NAME for
  300. that frame; otherwise, it uses the selected frame.
  301. If FRAME is the symbol t, then the global, non-frame face is returned.
  302. If NAME is already a face, it is simply returned."
  303.   (or (internal-find-face name frame)
  304.       (internal-check-face name)))
  305.  
  306.  
  307. (defun internal-set-face-1 (face name value index frame)
  308.   (let ((inhibit-quit t))
  309.     (if (null frame)
  310.     (let ((frames (frame-list)))
  311.       (while frames
  312.         (internal-set-face-1 (face-name face) name value index (car frames))
  313.         (setq frames (cdr frames)))
  314.       (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
  315.         index value)
  316.       value)
  317.       (or (eq frame t)
  318.       (set-face-attribute-internal (face-id face) name value frame))
  319.       (aset (internal-get-face face frame) index value))))
  320.  
  321.  
  322. (defun read-face-name (prompt)
  323.   (let (face)
  324.     (while (= (length face) 0)
  325.       (setq face (completing-read prompt
  326.                   (mapcar '(lambda (x) (list (symbol-name x)))
  327.                       (face-list))
  328.                   nil t)))
  329.     (intern face)))
  330.  
  331. (defun internal-face-interactive (what &optional bool)
  332.   (let* ((fn (intern (concat "face-" what)))
  333.      (prompt (concat "Set " what " of face"))
  334.      (face (read-face-name (concat prompt ": ")))
  335.      (default (if (fboundp fn)
  336.               (or (funcall fn face (selected-frame))
  337.               (funcall fn 'default (selected-frame)))))
  338.      (value (if bool
  339.             (y-or-n-p (concat "Should face " (symbol-name face)
  340.                       " be " bool "? "))
  341.           (read-string (concat prompt " " (symbol-name face) " to: ")
  342.                    default))))
  343.     (list face (if (equal value "") nil value))))
  344.  
  345.  
  346.  
  347. (defun make-face (name)
  348.   "Define a new FACE on all frames.  
  349. You can modify the font, color, etc of this face with the set-face- functions.
  350. If the face already exists, it is unmodified."
  351.   (interactive "SMake face: ")
  352.   (or (internal-find-face name)
  353.       (let ((face (make-vector 8 nil)))
  354.     (aset face 0 'face)
  355.     (aset face 1 name)
  356.     (let* ((frames (frame-list))
  357.            (inhibit-quit t)
  358.            (id (internal-next-face-id)))
  359.       (make-face-internal id)
  360.       (aset face 2 id)
  361.       (while frames
  362.         (set-frame-face-alist (car frames)
  363.                   (cons (cons name (copy-sequence face))
  364.                     (frame-face-alist (car frames))))
  365.         (setq frames (cdr frames)))
  366.       (setq global-face-data (cons (cons name face) global-face-data)))
  367.     ;; when making a face after frames already exist
  368.     (if (or (eq window-system 'x) (eq window-system 'win32))
  369.         (make-face-x-resource-internal face))
  370.     ;; add to menu
  371.     (if (fboundp 'facemenu-add-new-face)
  372.         (facemenu-add-new-face name))
  373.     face))
  374.   name)
  375.  
  376. ;; Fill in a face by default based on X resources, for all existing frames.
  377. ;; This has to be done when a new face is made.
  378. (defun make-face-x-resource-internal (face &optional frame set-anyway)
  379.   (cond ((null frame)
  380.      (let ((frames (frame-list)))
  381.        (while frames
  382.          (if (or (eq (framep (car frames)) 'x) (eq (framep (car frames)) 'win32))
  383.          (make-face-x-resource-internal (face-name face)
  384.                         (car frames) set-anyway))
  385.          (setq frames (cdr frames)))))
  386.     (t
  387.      (setq face (internal-get-face (face-name face) frame))
  388.      ;;
  389.      ;; These are things like "attributeForeground" instead of simply
  390.      ;; "foreground" because people tend to do things like "*foreground",
  391.      ;; which would cause all faces to be fully qualified, making faces
  392.      ;; inherit attributes in a non-useful way.  So we've made them slightly
  393.      ;; less obvious to specify in order to make them work correctly in
  394.      ;; more random environments.
  395.      ;;
  396.      ;; I think these should be called "face.faceForeground" instead of
  397.      ;; "face.attributeForeground", but they're the way they are for
  398.      ;; hysterical reasons.
  399.      ;; 
  400.      (let* ((name (symbol-name (face-name face)))
  401.         (fn  (or (x-get-resource (concat name ".attributeFont")
  402.                      "Face.AttributeFont")
  403.              (and set-anyway (face-font face))))
  404.         (fg  (or (x-get-resource (concat name ".attributeForeground")
  405.                      "Face.AttributeForeground")
  406.              (and set-anyway (face-foreground face))))
  407.         (bg  (or (x-get-resource (concat name ".attributeBackground")
  408.                      "Face.AttributeBackground")
  409.              (and set-anyway (face-background face))))
  410.         (bgp (or (x-get-resource (concat name ".attributeStipple")
  411.                      "Face.AttributeStipple")
  412.              (x-get-resource (concat name ".attributeBackgroundPixmap")
  413.                      "Face.AttributeBackgroundPixmap")
  414.              (and set-anyway (face-stipple face))))
  415.         (ulp (let ((resource (x-get-resource
  416.                       (concat name ".attributeUnderline")
  417.                       "Face.AttributeUnderline")))
  418.                (if resource
  419.                (member (downcase resource) '("on" "true"))
  420.              (and set-anyway (face-underline-p face)))))
  421.         )
  422.        (if fn
  423.            (condition-case ()
  424.            (cond ((string= fn "italic")
  425.               (make-face-italic face))
  426.              ((string= fn "bold")
  427.               (make-face-bold face))
  428.              ((string= fn "bold-italic")
  429.               (make-face-bold-italic face))
  430.              (t
  431.               (set-face-font face fn frame)))
  432.          (error
  433.           (if (member fn '("italic" "bold" "bold-italic"))
  434.               (message "no %s version found for face `%s'" fn name)
  435.             (message "font `%s' not found for face `%s'" fn name)))))
  436.        (if fg
  437.            (condition-case ()
  438.            (set-face-foreground face fg frame)
  439.          (error (message "color `%s' not allocated for face `%s'" fg name))))
  440.        (if bg
  441.            (condition-case ()
  442.            (set-face-background face bg frame)
  443.          (error (message "color `%s' not allocated for face `%s'" bg name))))
  444.        (if bgp
  445.            (condition-case ()
  446.            (set-face-stipple face bgp frame)
  447.          (error (message "pixmap `%s' not found for face `%s'" bgp name))))
  448.        (if (or ulp set-anyway)
  449.            (set-face-underline-p face ulp frame))
  450.        )))
  451.   face)
  452.  
  453. (defun copy-face (old-face new-face &optional frame new-frame)
  454.   "Define a face just like OLD-FACE, with name NEW-FACE.
  455. If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
  456. If it doesn't already exist, it is created.
  457.  
  458. If the optional argument FRAME is given as a frame,
  459. NEW-FACE is changed on FRAME only.
  460. If FRAME is t, the frame-independent default specification for OLD-FACE
  461. is copied to NEW-FACE.
  462. If FRAME is nil, copying is done for the frame-independent defaults
  463. and for each existing frame.
  464. If the optional fourth argument NEW-FRAME is given, 
  465. copy the information from face OLD-FACE on frame FRAME
  466. to NEW-FACE on frame NEW-FRAME."
  467.   (or new-frame (setq new-frame frame))
  468.   (let ((inhibit-quit t))
  469.     (if (null frame)
  470.     (let ((frames (frame-list)))
  471.       (while frames
  472.         (copy-face old-face new-face (car frames))
  473.         (setq frames (cdr frames)))
  474.       (copy-face old-face new-face t))
  475.       (setq old-face (internal-get-face old-face frame))
  476.       (setq new-face (or (internal-find-face new-face new-frame)
  477.              (make-face new-face)))
  478.       (condition-case nil
  479.       ;; A face that has a global symbolic font modifier such as `bold'
  480.       ;; might legitimately get an error here.
  481.       ;; Use the frame's default font in that case.
  482.       (set-face-font new-face (face-font old-face frame) new-frame)
  483.     (error
  484.      (set-face-font new-face nil new-frame)))
  485.       (set-face-foreground new-face (face-foreground old-face frame) new-frame)
  486.       (set-face-background new-face (face-background old-face frame) new-frame)
  487.       (set-face-stipple new-face
  488.             (face-stipple old-face frame)
  489.             new-frame)
  490.       (set-face-underline-p new-face (face-underline-p old-face frame)
  491.                 new-frame))
  492.     new-face))
  493.  
  494. (defun face-equal (face1 face2 &optional frame)
  495.   "True if the faces FACE1 and FACE2 display in the same way."
  496.   (setq face1 (internal-get-face face1 frame)
  497.     face2 (internal-get-face face2 frame))
  498.   (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
  499.        (equal (face-background face1 frame) (face-background face2 frame))
  500.        (equal (face-font face1 frame) (face-font face2 frame))
  501.        (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
  502.        (equal (face-stipple face1 frame)
  503.           (face-stipple face2 frame))))
  504.  
  505. (defun face-differs-from-default-p (face &optional frame)
  506.   "True if face FACE displays differently from the default face, on FRAME.
  507. A face is considered to be ``the same'' as the default face if it is 
  508. actually specified in the same way (equivalent fonts, etc) or if it is 
  509. fully unspecified, and thus inherits the attributes of any face it 
  510. is displayed on top of.
  511.  
  512. The optional argument FRAME specifies which frame to test;
  513. if FRAME is t, test the default for new frames.
  514. If FRAME is nil or omitted, test the selected frame."
  515.   (let ((default (internal-get-face 'default frame)))
  516.     (setq face (internal-get-face face frame))
  517.     (not (and (or (equal (face-foreground default frame)
  518.              (face-foreground face frame))
  519.           (null (face-foreground face frame)))
  520.           (or (equal (face-background default frame)
  521.              (face-background face frame))
  522.           (null (face-background face frame)))
  523.           (or (equal (face-font default frame) (face-font face frame))
  524.           (null (face-font face frame)))
  525.           (or (equal (face-stipple default frame)
  526.              (face-stipple face frame))
  527.           (null (face-stipple face frame)))
  528.           (equal (face-underline-p default frame)
  529.              (face-underline-p face frame))
  530.           ))))
  531.  
  532. (defun face-nontrivial-p (face &optional frame)
  533.   "True if face FACE has some non-nil attribute.
  534. The optional argument FRAME specifies which frame to test;
  535. if FRAME is t, test the default for new frames.
  536. If FRAME is nil or omitted, test the selected frame."
  537.   (setq face (internal-get-face face frame))
  538.   (or (face-foreground face frame)
  539.       (face-background face frame)
  540.       (face-font face frame)
  541.       (face-stipple face frame)
  542.       (face-underline-p face frame)))
  543.  
  544.  
  545. (defun invert-face (face &optional frame)
  546.   "Swap the foreground and background colors of face FACE.
  547. If the face doesn't specify both foreground and background, then
  548. set its foreground and background to the default background and foreground."
  549.   (interactive (list (read-face-name "Invert face: ")))
  550.   (setq face (internal-get-face face frame))
  551.   (let ((fg (face-foreground face frame))
  552.     (bg (face-background face frame)))
  553.     (if (or fg bg)
  554.     (progn
  555.       (set-face-foreground face bg frame)
  556.       (set-face-background face fg frame))
  557.       (set-face-foreground face (or (face-background 'default frame)
  558.                     (cdr (assq 'background-color (frame-parameters frame))))
  559.                frame)
  560.       (set-face-background face (or (face-foreground 'default frame)
  561.                     (cdr (assq 'foreground-color (frame-parameters frame))))
  562.                frame)))
  563.   face)
  564.  
  565.  
  566. (defun internal-try-face-font (face font &optional frame)
  567.   "Like set-face-font, but returns nil on failure instead of an error."
  568.   (condition-case ()
  569.       (set-face-font face font frame)
  570.     (error nil)))
  571.  
  572. ;; Manipulating font names.
  573.  
  574. (defconst x-font-regexp nil)
  575. (defconst x-font-regexp-head nil)
  576. (defconst x-font-regexp-weight nil)
  577. (defconst x-font-regexp-slant nil)
  578.  
  579. (defconst x-font-regexp-weight-subnum 1)
  580. (defconst x-font-regexp-slant-subnum 2)
  581. (defconst x-font-regexp-swidth-subnum 3)
  582. (defconst x-font-regexp-adstyle-subnum 4)
  583.  
  584. ;;; Regexps matching font names in "Host Portable Character Representation."
  585. ;;;
  586. (let ((-         "[-?]")
  587.       (foundry        "[^-]+")
  588.       (family         "[^-]+")
  589.       (weight        "\\(bold\\|demibold\\|medium\\)")        ; 1
  590. ;     (weight\?        "\\(\\*\\|bold\\|demibold\\|medium\\|\\)")    ; 1
  591.       (weight\?        "\\([^-]*\\)")                    ; 1
  592.       (slant        "\\([ior]\\)")                    ; 2
  593. ;     (slant\?        "\\([ior?*]?\\)")                ; 2
  594.       (slant\?        "\\([^-]?\\)")                    ; 2
  595. ;     (swidth        "\\(\\*\\|normal\\|semicondensed\\|\\)")    ; 3
  596.       (swidth        "\\([^-]*\\)")                    ; 3
  597. ;     (adstyle        "\\(\\*\\|sans\\|\\)")                ; 4
  598.       (adstyle        "\\([^-]*\\)")                    ; 4
  599.       (pixelsize    "[0-9]+")
  600.       (pointsize    "[0-9][0-9]+")
  601.       (resx        "[0-9][0-9]+")
  602.       (resy        "[0-9][0-9]+")
  603.       (spacing        "[cmp?*]")
  604.       (avgwidth        "[0-9]+")
  605.       (registry        "[^-]+")
  606.       (encoding        "[^-]+")
  607.       )
  608.   (setq x-font-regexp
  609.     (concat "\\`\\*?[-?*]"
  610.         foundry - family - weight\? - slant\? - swidth - adstyle -
  611.         pixelsize - pointsize - resx - resy - spacing - avgwidth -
  612.         registry - encoding "\\*?\\'"
  613.         ))
  614.   (setq x-font-regexp-head
  615.     (concat "\\`[-?*]" foundry - family - weight\? - slant\?
  616.         "\\([-*?]\\|\\'\\)"))
  617.   (setq x-font-regexp-slant (concat - slant -))
  618.   (setq x-font-regexp-weight (concat - weight -))
  619.   nil)        
  620.  
  621. (defun x-resolve-font-name (pattern &optional face frame)
  622.   "Return a font name matching PATTERN.
  623. All wildcards in PATTERN become substantiated.
  624. If PATTERN is nil, return the name of the frame's base font, which never
  625. contains wildcards.
  626. Given optional arguments FACE and FRAME, return a font which is
  627. also the same size as FACE on FRAME, or fail."
  628.   (or (symbolp face)
  629.       (setq face (face-name face)))
  630.   (and (eq frame t)
  631.        (setq frame nil))
  632.   (if pattern
  633.       ;; Note that x-list-fonts has code to handle a face with nil as its font.
  634.       (let ((fonts (x-list-fonts pattern face frame)))
  635.     (or fonts
  636.         (if face
  637.         (if (string-match "\\*" pattern)
  638.             (if (null (face-font face))
  639.             (error "No matching fonts are the same height as the frame default font")
  640.               (error "No matching fonts are the same height as face `%s'" face))
  641.           (if (null (face-font face))
  642.               (error "Height of font `%s' doesn't match the frame default font"
  643.                  pattern)
  644.             (error "Height of font `%s' doesn't match face `%s'"
  645.                pattern face)))
  646.           (error "No fonts match `%s'" pattern)))
  647.     (car fonts))
  648.     (cdr (assq 'font (frame-parameters (selected-frame))))))
  649.  
  650. (defun x-frob-font-weight (font which)
  651.   (let ((case-fold-search t))
  652.     (cond ((string-match x-font-regexp font)
  653.        (concat (substring font 0
  654.                   (match-beginning x-font-regexp-weight-subnum))
  655.            which
  656.            (substring font (match-end x-font-regexp-weight-subnum)
  657.                   (match-beginning x-font-regexp-adstyle-subnum))
  658.            ;; Replace the ADD_STYLE_NAME field with *
  659.            ;; because the info in it may not be the same
  660.            ;; for related fonts.
  661.            "*"
  662.            (substring font (match-end x-font-regexp-adstyle-subnum))))
  663.       ((string-match x-font-regexp-head font)
  664.        (concat (substring font 0 (match-beginning 1)) which
  665.            (substring font (match-end 1))))
  666.       ((string-match x-font-regexp-weight font)
  667.        (concat (substring font 0 (match-beginning 1)) which
  668.            (substring font (match-end 1)))))))
  669.  
  670. (defun x-frob-font-slant (font which)
  671.   (let ((case-fold-search t))
  672.     (cond ((string-match x-font-regexp font)
  673.        (concat (substring font 0
  674.                   (match-beginning x-font-regexp-slant-subnum))
  675.            which
  676.            (substring font (match-end x-font-regexp-slant-subnum)
  677.                   (match-beginning x-font-regexp-adstyle-subnum))
  678.            ;; Replace the ADD_STYLE_NAME field with *
  679.            ;; because the info in it may not be the same
  680.            ;; for related fonts.
  681.            "*"
  682.            (substring font (match-end x-font-regexp-adstyle-subnum))))
  683.       ((string-match x-font-regexp-head font)
  684.        (concat (substring font 0 (match-beginning 2)) which
  685.            (substring font (match-end 2))))
  686.       ((string-match x-font-regexp-slant font)
  687.        (concat (substring font 0 (match-beginning 1)) which
  688.            (substring font (match-end 1)))))))
  689.  
  690. (defun x-make-font-bold (font)
  691.   "Given an X font specification, make a bold version of it.
  692. If that can't be done, return nil."
  693.   (x-frob-font-weight font "bold"))
  694.  
  695. (defun x-make-font-demibold (font)
  696.   "Given an X font specification, make a demibold version of it.
  697. If that can't be done, return nil."
  698.   (x-frob-font-weight font "demibold"))
  699.  
  700. (defun x-make-font-unbold (font)
  701.   "Given an X font specification, make a non-bold version of it.
  702. If that can't be done, return nil."
  703.   (x-frob-font-weight font "medium"))
  704.  
  705. (defun x-make-font-italic (font)
  706.   "Given an X font specification, make an italic version of it.
  707. If that can't be done, return nil."
  708.   (x-frob-font-slant font "i"))
  709.  
  710. (defun x-make-font-oblique (font) ; you say tomayto...
  711.   "Given an X font specification, make an oblique version of it.
  712. If that can't be done, return nil."
  713.   (x-frob-font-slant font "o"))
  714.  
  715. (defun x-make-font-unitalic (font)
  716.   "Given an X font specification, make a non-italic version of it.
  717. If that can't be done, return nil."
  718.   (x-frob-font-slant font "r"))
  719.  
  720. ;;; non-X-specific interface
  721.  
  722. (defun make-face-bold (face &optional frame noerror)
  723.   "Make the font of the given face be bold, if possible.  
  724. If NOERROR is non-nil, return nil on failure."
  725.   (interactive (list (read-face-name "Make which face bold: ")))
  726.   (if (and (eq frame t) (listp (face-font face t)))
  727.       (set-face-font face (if (memq 'italic (face-font face t))
  728.                   '(bold italic) '(bold))
  729.              t)
  730.     (let (font)
  731.       (if (null frame)
  732.       (let ((frames (frame-list)))
  733.         ;; Make this face bold in global-face-data.
  734.         (make-face-bold face t noerror)
  735.         ;; Make this face bold in each frame.
  736.         (while frames
  737.           (make-face-bold face (car frames) noerror)
  738.           (setq frames (cdr frames))))
  739.     (setq face (internal-get-face face frame))
  740.     (setq font (or (face-font face frame)
  741.                (face-font face t)))
  742.     (if (listp font)
  743.         (setq font nil))
  744.     (setq font (or font
  745.                (face-font 'default frame)
  746.                (cdr (assq 'font (frame-parameters frame)))))
  747.     (or (and font (make-face-bold-internal face frame font))
  748.         ;; We failed to find a bold version of the font.
  749.         noerror
  750.         (error "No bold version of %S" font))))))
  751.  
  752. (defun make-face-bold-internal (face frame font)
  753.   (let (f2)
  754.     (or (and (setq f2 (x-make-font-bold font))
  755.          (internal-try-face-font face f2 frame))
  756.     (and (setq f2 (x-make-font-demibold font))
  757.          (internal-try-face-font face f2 frame)))))
  758.  
  759. (defun make-face-italic (face &optional frame noerror)
  760.   "Make the font of the given face be italic, if possible.  
  761. If NOERROR is non-nil, return nil on failure."
  762.   (interactive (list (read-face-name "Make which face italic: ")))
  763.   (if (and (eq frame t) (listp (face-font face t)))
  764.       (set-face-font face (if (memq 'bold (face-font face t))
  765.                   '(bold italic) '(italic))
  766.              t)
  767.     (let (font)
  768.       (if (null frame)
  769.       (let ((frames (frame-list)))
  770.         ;; Make this face italic in global-face-data.
  771.         (make-face-italic face t noerror)
  772.         ;; Make this face italic in each frame.
  773.         (while frames
  774.           (make-face-italic face (car frames) noerror)
  775.           (setq frames (cdr frames))))
  776.     (setq face (internal-get-face face frame))
  777.     (setq font (or (face-font face frame)
  778.                (face-font face t)))
  779.     (if (listp font)
  780.         (setq font nil))
  781.     (setq font (or font
  782.                (face-font 'default frame)
  783.                (cdr (assq 'font (frame-parameters frame)))))
  784.     (or (and font (make-face-italic-internal face frame font))
  785.         ;; We failed to find an italic version of the font.
  786.         noerror
  787.         (error "No italic version of %S" font))))))
  788.  
  789. (defun make-face-italic-internal (face frame font)
  790.   (let (f2)
  791.     (or (and (setq f2 (x-make-font-italic font))
  792.          (internal-try-face-font face f2 frame))
  793.     (and (setq f2 (x-make-font-oblique font))
  794.          (internal-try-face-font face f2 frame)))))
  795.  
  796. (defun make-face-bold-italic (face &optional frame noerror)
  797.   "Make the font of the given face be bold and italic, if possible.  
  798. If NOERROR is non-nil, return nil on failure."
  799.   (interactive (list (read-face-name "Make which face bold-italic: ")))
  800.   (if (and (eq frame t) (listp (face-font face t)))
  801.       (set-face-font face '(bold italic) t)
  802.     (let (font)
  803.       (if (null frame)
  804.       (let ((frames (frame-list)))
  805.         ;; Make this face bold-italic in global-face-data.
  806.         (make-face-bold-italic face t noerror)
  807.         ;; Make this face bold in each frame.
  808.         (while frames
  809.           (make-face-bold-italic face (car frames) noerror)
  810.           (setq frames (cdr frames))))
  811.     (setq face (internal-get-face face frame))
  812.     (setq font (or (face-font face frame)
  813.                (face-font face t)))
  814.     (if (listp font)
  815.         (setq font nil))
  816.     (setq font (or font
  817.                (face-font 'default frame)
  818.                (cdr (assq 'font (frame-parameters frame)))))
  819.     (or (and font (make-face-bold-italic-internal face frame font))
  820.         ;; We failed to find a bold italic version.
  821.         noerror
  822.         (error "No bold italic version of %S" font))))))
  823.  
  824. (defun make-face-bold-italic-internal (face frame font)
  825.   (let (f2 f3)
  826.     (or (and (setq f2 (x-make-font-italic font))
  827.          (not (equal font f2))
  828.          (setq f3 (x-make-font-bold f2))
  829.          (not (equal f2 f3))
  830.          (internal-try-face-font face f3 frame))
  831.     (and (setq f2 (x-make-font-oblique font))
  832.          (not (equal font f2))
  833.          (setq f3 (x-make-font-bold f2))
  834.          (not (equal f2 f3))
  835.          (internal-try-face-font face f3 frame))
  836.     (and (setq f2 (x-make-font-italic font))
  837.          (not (equal font f2))
  838.          (setq f3 (x-make-font-demibold f2))
  839.          (not (equal f2 f3))
  840.          (internal-try-face-font face f3 frame))
  841.     (and (setq f2 (x-make-font-oblique font))
  842.          (not (equal font f2))
  843.          (setq f3 (x-make-font-demibold f2))
  844.          (not (equal f2 f3))
  845.          (internal-try-face-font face f3 frame)))))
  846.  
  847. (defun make-face-unbold (face &optional frame noerror)
  848.   "Make the font of the given face be non-bold, if possible.  
  849. If NOERROR is non-nil, return nil on failure."
  850.   (interactive (list (read-face-name "Make which face non-bold: ")))
  851.   (if (and (eq frame t) (listp (face-font face t)))
  852.       (set-face-font face (if (memq 'italic (face-font face t))
  853.                   '(italic) nil)
  854.              t)
  855.     (let (font font1)
  856.       (if (null frame)
  857.       (let ((frames (frame-list)))
  858.         ;; Make this face unbold in global-face-data.
  859.         (make-face-unbold face t noerror)
  860.         ;; Make this face unbold in each frame.
  861.         (while frames
  862.           (make-face-unbold face (car frames) noerror)
  863.           (setq frames (cdr frames))))
  864.     (setq face (internal-get-face face frame))
  865.     (setq font1 (or (face-font face frame)
  866.             (face-font face t)))
  867.     (if (listp font1)
  868.         (setq font1 nil))
  869.     (setq font1 (or font1
  870.             (face-font 'default frame)
  871.             (cdr (assq 'font (frame-parameters frame)))))
  872.     (setq font (and font1 (x-make-font-unbold font1)))
  873.     (or (if font (internal-try-face-font face font frame))
  874.         noerror
  875.         (error "No unbold version of %S" font1))))))
  876.  
  877. (defun make-face-unitalic (face &optional frame noerror)
  878.   "Make the font of the given face be non-italic, if possible.  
  879. If NOERROR is non-nil, return nil on failure."
  880.   (interactive (list (read-face-name "Make which face non-italic: ")))
  881.   (if (and (eq frame t) (listp (face-font face t)))
  882.       (set-face-font face (if (memq 'bold (face-font face t))
  883.                   '(bold) nil)
  884.              t)
  885.     (let (font font1)
  886.       (if (null frame)
  887.       (let ((frames (frame-list)))
  888.         ;; Make this face unitalic in global-face-data.
  889.         (make-face-unitalic face t noerror)
  890.         ;; Make this face unitalic in each frame.
  891.         (while frames
  892.           (make-face-unitalic face (car frames) noerror)
  893.           (setq frames (cdr frames))))
  894.     (setq face (internal-get-face face frame))
  895.     (setq font1 (or (face-font face frame)
  896.             (face-font face t)))
  897.     (if (listp font1)
  898.         (setq font1 nil))
  899.     (setq font1 (or font1
  900.             (face-font 'default frame)
  901.             (cdr (assq 'font (frame-parameters frame)))))
  902.     (setq font (and font1 (x-make-font-unitalic font1)))
  903.     (or (if font (internal-try-face-font face font frame))
  904.         noerror
  905.         (error "No unitalic version of %S" font1))))))
  906.  
  907. (defvar list-faces-sample-text
  908.   "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  909.   "*Text string to display as the sample text for `list-faces-display'.")
  910.  
  911. ;; The name list-faces would be more consistent, but let's avoid a conflict
  912. ;; with Lucid, which uses that name differently.
  913. (defun list-faces-display ()
  914.   "List all faces, using the same sample text in each.
  915. The sample text is a string that comes from the variable
  916. `list-faces-sample-text'.
  917.  
  918. It is possible to give a particular face name different appearances in
  919. different frames.  This command shows the appearance in the
  920. selected frame."
  921.   (interactive)
  922.   (let ((faces (sort (face-list) (function string-lessp)))
  923.     (face nil)
  924.     (frame (selected-frame))
  925.     disp-frame window)
  926.     (with-output-to-temp-buffer "*Faces*"
  927.       (save-excursion
  928.     (set-buffer standard-output)
  929.     (setq truncate-lines t)
  930.     (while faces
  931.       (setq face (car faces))
  932.       (setq faces (cdr faces))
  933.       (insert (format "%25s " (symbol-name face)))
  934.       (let ((beg (point)))
  935.         (insert list-faces-sample-text)
  936.         (insert "\n")
  937.         (put-text-property beg (1- (point)) 'face face)
  938.         ;; If the sample text has multiple lines, line up all of them.
  939.         (goto-char beg)
  940.         (forward-line 1)
  941.         (while (not (eobp))
  942.           (insert "                          ")
  943.           (forward-line 1))))
  944.     (goto-char (point-min))))
  945.     ;; If the *Faces* buffer appears in a different frame,
  946.     ;; copy all the face definitions from FRAME,
  947.     ;; so that the display will reflect the frame that was selected.
  948.     (setq window (get-buffer-window (get-buffer "*Faces*") t))
  949.     (setq disp-frame (if window (window-frame window)
  950.                (car (frame-list))))
  951.     (or (eq frame disp-frame)
  952.     (let ((faces (face-list)))
  953.       (while faces
  954.         (copy-face (car faces) (car faces) frame disp-frame)
  955.         (setq faces (cdr faces)))))))
  956.  
  957. (defun describe-face (face)
  958.   "Display the properties of face FACE."
  959.   (interactive (list (read-face-name "Describe face: ")))
  960.   (with-output-to-temp-buffer "*Help*"
  961.     (princ "Properties of face `")
  962.     (princ (face-name face))
  963.     (princ "':") (terpri)
  964.     (princ "Foreground: ") (princ (face-foreground face)) (terpri)
  965.     (princ "Background: ") (princ (face-background face)) (terpri)
  966.     (princ "      Font: ") (princ (face-font face)) (terpri)
  967.     (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
  968.     (princ "   Stipple: ") (princ (or (face-stipple face) "none"))))
  969.  
  970. ;;; Make the standard faces.
  971. ;;; The C code knows the default and modeline faces as faces 0 and 1,
  972. ;;; so they must be the first two faces made.
  973. (defun face-initialize ()
  974.   (make-face 'default)
  975.   (make-face 'modeline)
  976.   (make-face 'highlight)
  977.  
  978.   ;; These aren't really special in any way, but they're nice to have around.
  979.  
  980.   (make-face 'bold)
  981.   (make-face 'italic)
  982.   (make-face 'bold-italic)
  983.   (make-face 'region)
  984.   (make-face 'secondary-selection)
  985.   (make-face 'underline)
  986.  
  987.   (setq region-face (face-id 'region))
  988.  
  989.   ;; Specify the global properties of these faces
  990.   ;; so they will come out right on new frames.
  991.  
  992.   (make-face-bold 'bold t)
  993.   (make-face-italic 'italic t)
  994.   (make-face-bold-italic 'bold-italic t)
  995.  
  996.   (set-face-background 'highlight '("darkseagreen2" "green" t) t)
  997.   (set-face-background 'region '("gray" underline) t)
  998.   (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
  999.   (set-face-background 'modeline '(t) t)
  1000.   (set-face-underline-p 'underline t t)
  1001.  
  1002.   ;; Set up the faces of all existing X Window frames
  1003.   ;; from those global properties, unless already set in a given frame.
  1004.  
  1005.   (let ((frames (frame-list)))
  1006.     (while frames
  1007.       (if (not (memq (framep (car frames)) '(t nil)))
  1008.       (let ((frame (car frames))
  1009.         (rest global-face-data))
  1010.         (while rest
  1011.           (let ((face (car (car rest))))
  1012.         (or (face-differs-from-default-p face)
  1013.             (face-fill-in face (cdr (car rest)) frame)))
  1014.           (setq rest (cdr rest)))))
  1015.       (setq frames (cdr frames)))))
  1016.  
  1017.  
  1018. ;; Like x-create-frame but also set up the faces.
  1019.  
  1020. (defun x-create-frame-with-faces (&optional parameters)
  1021.   ;; Read this frame's geometry resource, if it has an explicit name,
  1022.   ;; and put the specs into PARAMETERS.
  1023.   (let* ((name (or (cdr (assq 'name parameters))
  1024.            (cdr (assq 'name default-frame-alist))))
  1025.      (x-resource-name name)
  1026.      (res-geometry (if name (x-get-resource "geometry" "Geometry")))
  1027.      parsed)
  1028.     (if res-geometry
  1029.     (progn
  1030.       (setq parsed (x-parse-geometry res-geometry))
  1031.       ;; If the resource specifies a position,
  1032.       ;; call the position and size "user-specified".
  1033.       (if (or (assq 'top parsed) (assq 'left parsed))
  1034.           (setq parsed (cons '(user-position . t)
  1035.                  (cons '(user-size . t) parsed))))
  1036.       ;; Put the geometry parameters at the end.
  1037.       ;; Copy default-frame-alist so that they go after it.
  1038.       (setq parameters (append parameters
  1039.                    default-frame-alist
  1040.                    parsed)))))
  1041.   (let (frame)
  1042.     (if (null global-face-data)
  1043.     (setq frame (x-create-frame parameters))
  1044.       (let* ((visibility-spec (assq 'visibility parameters))
  1045.          (faces (copy-alist global-face-data))
  1046.          success
  1047.          (rest faces))
  1048.     (setq frame (x-create-frame (cons '(visibility . nil) parameters)))
  1049.     (unwind-protect
  1050.         (progn
  1051.           (set-frame-face-alist frame faces)
  1052.  
  1053.           (if (cdr (or (assq 'reverse parameters)
  1054.                (assq 'reverse default-frame-alist)
  1055.                (let ((resource (x-get-resource "reverseVideo"
  1056.                                "ReverseVideo")))
  1057.                  (if resource
  1058.                  (cons nil (member (downcase resource)
  1059.                            '("on" "true")))))))
  1060.           (let* ((params (frame-parameters frame))
  1061.              (bg (cdr (assq 'foreground-color params)))
  1062.              (fg (cdr (assq 'background-color params))))
  1063.             (modify-frame-parameters frame
  1064.                          (list (cons 'foreground-color fg)
  1065.                            (cons 'background-color bg)))
  1066.             (if (equal bg (cdr (assq 'border-color params)))
  1067.             (modify-frame-parameters frame
  1068.                          (list (cons 'border-color fg))))
  1069.             (if (equal bg (cdr (assq 'mouse-color params)))
  1070.             (modify-frame-parameters frame
  1071.                          (list (cons 'mouse-color fg))))
  1072.             (if (equal bg (cdr (assq 'cursor-color params)))
  1073.             (modify-frame-parameters frame
  1074.                          (list (cons 'cursor-color fg))))))
  1075.           ;; Copy the vectors that represent the faces.
  1076.           ;; Also fill them in from X resources.
  1077.           (while rest
  1078.         (let ((global (cdr (car rest))))
  1079.           (setcdr (car rest) (vector 'face
  1080.                          (face-name (cdr (car rest)))
  1081.                          (face-id (cdr (car rest)))
  1082.                          nil nil nil nil nil))
  1083.           (face-fill-in (car (car rest)) global frame))
  1084.         (make-face-x-resource-internal (cdr (car rest)) frame t)
  1085.         (setq rest (cdr rest)))
  1086.           (if (null visibility-spec)
  1087.           (make-frame-visible frame)
  1088.         (modify-frame-parameters frame (list visibility-spec)))
  1089.           (setq success t))
  1090.       (or success
  1091.           (delete-frame frame)))))
  1092.     ;; Set up the background-mode frame parameter
  1093.     ;; so that programs can decide good ways of highlighting
  1094.     ;; on this frame.
  1095.     (let ((bg-resource (x-get-resource ".backgroundMode"
  1096.                        "BackgroundMode"))
  1097.       (params (frame-parameters frame))
  1098.       (bg-mode))
  1099.       (setq bg-mode
  1100.         (cond (bg-resource (intern (downcase bg-resource)))
  1101.           ((< (apply '+ (x-color-values
  1102.                  (cdr (assq 'background-color params))
  1103.                  frame))
  1104.               ;; Just looking at the screen,
  1105.               ;; colors whose values add up to .6 of the white total
  1106.               ;; still look dark to me.
  1107.               (* (apply '+ (x-color-values "white" frame)) .6))
  1108.            'dark)
  1109.           (t 'light)))
  1110.       (modify-frame-parameters frame
  1111.                    (list (cons 'background-mode bg-mode)
  1112.                      (cons 'display-type
  1113.                        (cond ((x-display-color-p frame)
  1114.                           'color)
  1115.                          ((x-display-grayscale-p frame)
  1116.                           'grayscale)
  1117.                          (t 'mono))))))
  1118.     frame))
  1119.  
  1120. ;; Update a frame's faces when we change its default font.
  1121. (defun frame-update-faces (frame)
  1122.   (let* ((faces global-face-data)
  1123.      (rest faces))
  1124.     (while rest
  1125.       (let* ((face (car (car rest)))
  1126.          (font (face-font face t)))
  1127.     (if (listp font)
  1128.         (let ((bold (memq 'bold font))
  1129.           (italic (memq 'italic font)))
  1130.           ;; Ignore any previous (string-valued) font, it might not even
  1131.           ;; be the right size anymore.
  1132.           (set-face-font face nil frame)
  1133.           (cond ((and bold italic)
  1134.              (make-face-bold-italic face frame t))
  1135.             (bold
  1136.              (make-face-bold face frame t))
  1137.             (italic
  1138.              (make-face-italic face frame t)))))
  1139.       (setq rest (cdr rest)))
  1140.     frame)))
  1141.  
  1142. ;; Update the colors of FACE, after FRAME's own colors have been changed.
  1143. ;; This applies only to faces with global color specifications
  1144. ;; that are not simple constants.
  1145. (defun frame-update-face-colors (frame)
  1146.   (let ((faces global-face-data))
  1147.     (while faces
  1148.       (condition-case nil
  1149.       (let* ((data (cdr (car faces)))
  1150.          (face (car (car faces)))
  1151.          (foreground (face-foreground data))
  1152.          (background (face-background data)))
  1153.         ;; If the global spec is a specific color,
  1154.         ;; which doesn't depend on the frame's attributes,
  1155.         ;; we don't need to recalculate it now.
  1156.         (or (listp foreground)
  1157.         (setq foreground nil))
  1158.         (or (listp background)
  1159.         (setq background nil))
  1160.         ;; If we are going to frob this face at all,
  1161.         ;; reinitialize it first.
  1162.         (if (or foreground background)
  1163.         (progn (set-face-foreground face nil frame)
  1164.                (set-face-background face nil frame)))
  1165.         (if foreground
  1166.         (face-try-color-list 'set-face-foreground
  1167.                      face foreground frame))
  1168.         (if background
  1169.         (face-try-color-list 'set-face-background
  1170.                      face background frame)))
  1171.     (error nil))
  1172.       (setq faces (cdr faces)))))
  1173.  
  1174. ;; Fill in the face FACE from frame-independent face data DATA.
  1175. ;; DATA should be the non-frame-specific ("global") face vector
  1176. ;; for the face.  FACE should be a face name or face object.
  1177. ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
  1178. (defun face-fill-in (face data frame)
  1179.   (condition-case nil
  1180.       (let ((foreground (face-foreground data))
  1181.         (background (face-background data))
  1182.         (font (face-font data))
  1183.         (stipple (face-stipple data)))
  1184.     (set-face-underline-p face (face-underline-p data) frame)
  1185.     (if foreground
  1186.         (face-try-color-list 'set-face-foreground
  1187.                  face foreground frame))
  1188.     (if background
  1189.         (face-try-color-list 'set-face-background
  1190.                  face background frame))
  1191.     (if (listp font)
  1192.         (let ((bold (memq 'bold font))
  1193.           (italic (memq 'italic font)))
  1194.           (cond ((and bold italic)
  1195.              (make-face-bold-italic face frame))
  1196.             (bold
  1197.              (make-face-bold face frame))
  1198.             (italic
  1199.              (make-face-italic face frame))))
  1200.       (if font
  1201.           (set-face-font face font frame)))
  1202.     (if stipple
  1203.         (set-face-stipple face stipple frame)))
  1204.     (error nil)))
  1205.  
  1206. ;; Assuming COLOR is a valid color name,
  1207. ;; return t if it can be displayed on FRAME.
  1208. (defun face-color-supported-p (frame color background-p)
  1209.   (and window-system
  1210.        (or (x-display-color-p frame)
  1211.        ;; A black-and-white display can implement these.
  1212.        (member color '("black" "white"))
  1213.        ;; A black-and-white display can fake gray for background.
  1214.        (and background-p
  1215.         (face-color-gray-p color frame))
  1216.        ;; A grayscale display can implement colors that are gray (more or less).
  1217.        (and (x-display-grayscale-p frame)
  1218.         (face-color-gray-p color frame)))))
  1219.  
  1220. ;; Use FUNCTION to store a color in FACE on FRAME.
  1221. ;; COLORS is either a single color or a list of colors.
  1222. ;; If it is a list, try the colors one by one until one of them
  1223. ;; succeeds.  We signal an error only if all the colors failed.
  1224. ;; t as COLORS or as an element of COLORS means to invert the face.
  1225. ;; That can't fail, so any subsequent elements after the t are ignored.
  1226. (defun face-try-color-list (function face colors frame)
  1227.   (if (stringp colors)
  1228.       (if (face-color-supported-p frame colors
  1229.                   (eq function 'set-face-background))
  1230.       (funcall function face colors frame))
  1231.     (if (eq colors t)
  1232.     (invert-face face frame)
  1233.       (let (done)
  1234.     (while (and colors (not done))
  1235.       (if (or (memq (car colors) '(t underline))
  1236.           (face-color-supported-p frame (car colors)
  1237.                       (eq function 'set-face-background)))
  1238.           (if (cdr colors)
  1239.           ;; If there are more colors to try, catch errors
  1240.           ;; and set `done' if we succeed.
  1241.           (condition-case nil
  1242.               (progn
  1243.             (cond ((eq (car colors) t)
  1244.                    (invert-face face frame))
  1245.                   ((eq (car colors) 'underline)
  1246.                    (set-face-underline-p face t frame))
  1247.                   (t
  1248.                    (funcall function face (car colors) frame)))
  1249.             (setq done t))
  1250.             (error nil))
  1251.         ;; If this is the last color, let the error get out if it fails.
  1252.         ;; If it succeeds, we will exit anyway after this iteration.
  1253.         (cond ((eq (car colors) t)
  1254.                (invert-face face frame))
  1255.               ((eq (car colors) 'underline)
  1256.                (set-face-underline-p face t frame))
  1257.               (t
  1258.                (funcall function face (car colors) frame)))))
  1259.       (setq colors (cdr colors)))))))
  1260.  
  1261. ;; If we are already using x-window frames, initialize faces for them.
  1262. (if (or (eq (framep (selected-frame)) 'x) (eq (framep (selected-frame)) 'win32))
  1263.     (face-initialize))
  1264.  
  1265. (provide 'faces)
  1266.  
  1267. ;;; faces.el ends here
  1268.